<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <title>layui在线调试</title>
    <!--<link rel="stylesheet" href="//res.layui.com/layui/dist/css/layui.css?t=1542630986927" media="all">-->
    <link rel="stylesheet" href="/plugins/layui/css/layui.css" media="all">
    <link rel="stylesheet" type="text/css" href="/common/css/fs.css" media="all"/>
    <link rel="stylesheet" type="text/css" href="/plugins/ztree/css/zTreeStyle.css" media="all"/>

    <script src="/plugins/layui/layui.js"></script>
    <script src="/plugins/jquery/jquery.min.js?v=1.12.4"></script>
    <script src="/plugins/ztree/js/jquery.ztree.all.min.js"></script>
    <script src="/plugins/ztree/js/tree.js"></script>
    <script src="/common/js/my-jquery.js?v=1.12.4"></script>
    <script src="/common/js/common.js?v=1.12.4"></script>
    <script src="/common/js/page.js?v=1.12.4"></script>
    <style>
        body {
            margin: 10px;
        }
    </style>
</head>
<body>
<!-- 左边部门结构 -->
<div style="border:1px solid #dcdcdc;width:17%;height:665px;float:left;">
    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
        <legend>部门信息</legend>
    </fieldset>
    <div id="test1"></div>
    <div>
        <ul id="treeDemo" class="ztree"></ul>
    </div>
</div>

<!-- 右边用户管理table -->
<div style="width:81%;height:665px;float:left;">
    <form class="layui-form" action="" id="searchForm">
        <div class="layui-row">
            <div class="layui-inline">
                <div class="layui-input-inline">
                    <input type="tel" name="username" autocomplete="off" class="layui-input" placeholder="用户名">
                    <input type="tel" name="departId" id="departId" autocomplete="off" class="layui-input" placeholder="部门id" style="display:none">
                </div>
            </div>
            <div class="layui-inline">
                <div class="layui-input-inline">
                    <input type="tel" name="nickname" autocomplete="off" class="layui-input" placeholder="昵称">
                </div>
            </div>
            <div class="layui-inline">
                <button class="layui-btn layui-btn-normal" type="button" id="searchButton">搜索</button>
            </div>
        </div>
    </form>
    <table class="layui-hide" id="table-list" lay-filter="table-bar"></table>
    <!-- 头部工具栏模板： -->
    <script type="text/html" id="head-toolbar">
        <div class="layui-btn-container">
            <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
            <button class="layui-btn layui-btn-sm" lay-event="delete">删除</button>
            <button class="layui-btn layui-btn-sm" lay-event="edit">编辑</button>
        </div>
    </script>
    <!-- 行工具栏模板 table 中操作按钮-->
    <script type="text/html" id="oper-col">
        <!--<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">查看</a>-->
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
        <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="userRole">用户角色管理</a>
    </script>
</div>
<script>
    layui.use(['laydate', 'layer', 'table', 'carousel', 'upload', 'form', 'tree'], function () {
        var laydate = layui.laydate //日期
            , table = layui.table //表格
            , form = layui.form;//表单
        var usertTable = null;
        window.dataReload = function () {
            Page.initSearchForm($("#searchButton"), $("#searchForm"), usertTable, form);
        };
        //执行一个 table 实例
        usertTable = table.render({
            elem: '#table-list'
            , height: 720
            , url: '/user/list.json' //数据接口
            //, where: $("#form").serializeJson() //参数
            , response: {statusCode: 200}//200为成功状态码
            , parseData: function (res) { //res 即为原始返回的数据
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.records //解析数据列表
                };
            }
            , request: {
                pageName: 'current' //页码的参数名称，默认：page
                , limitName: 'size' //每页数据量的参数名，默认：limit
            }
            , title: '用户表'
            , page: true //开启分页
            //, page: {"layout": ['count', 'prev', 'page', 'next']} //开启分页
            , toolbar: '#head-toolbar' //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
            , defaultToolbar: []
            //, totalRow: true //开启合计行
            , limit: 10
            , cols: [[ //表头
                {type: 'checkbox', fixed: 'left'}
                , {field: 'id', title: 'ID', width: 120, sort: true, fixed: 'left', totalRowText: '合计：'}
                , {field: 'username', title: '用户名', width: 120}
                , {field: 'nickname', title: '昵称', width: 120, sort: true, totalRow: true}
                , {field: 'phone', title: '电话', width: 120}
                , {field: 'email', title: '邮箱', width: 120}
                , {templet: '#oper-col', title: '操作'}
            ]]
        });
        //初始化搜索
        Page.initSearchForm($("#searchButton"), $("#searchForm"), usertTable, form);
        //监听头工具栏事件
        table.on('toolbar(table-bar)', function (obj) { //注：toolbar 是工具条事件名，table-bar 是 table 原始容器的属性 lay-filter="对应的值"
            var checkStatus = table.checkStatus(obj.config.id)
                , data = checkStatus.data; //获取选中的数据
            switch (obj.event) {
                case 'add':
                    var url = "/user/add.html";
                    Common.topOpenDlg(url, "用户管理>新增", '27%', '42%');
                    break;
                case 'edit':
                    if (data.length === 0) {
                        layer.msg('请选择一行');
                    } else if (data.length > 1) {
                        layer.msg('只能同时编辑一个');
                    } else {
                        var url = "/user/edit.html?id=" + data[0].id;
                        Common.topOpenDlg(url, "用户管理>编辑", '25%', '42%');
                    }
                    break;
                case 'delete':
                    if (data.length === 0) {
                        layer.msg('请选择一行');
                    } else {
                        Common.deleteData("/user/delete.json", {id: data[0].id}, '/user/index.html');
                    }
                    break;
            }
        });
        //监听行工具事件
        table.on('tool(table-bar)', function (obj) { //注：tool 是工具条事件名，barDemo 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
                , layEvent = obj.event; //获得 lay-event 对应的值
            if (layEvent === 'edit') {
                var url = "/user/edit.html?id=" + data.id;
                Common.topOpenDlg(url, "用户管理>编辑", '25%', '42%');
            } else if (layEvent === 'del') {
                Common.deleteData("/user/delete.json", {id: data.id}, '');
            } else if (layEvent === 'userRole') {
                var url = "/userRole/userRole.html?userId=" + data.id;
                Common.topOpenDlg(url, "用户管理>用户角色管理", '40%', '60%');
            }
        });
        $.get('/depart/list.json', {}, function (data) {
            var dataList = data.data;
            var treeData = simpleListToTree(dataList)
            var zTreeObj;
            // zTree 的参数配置，深入使用请参考 API 文档（setting 配置详解）
            var setting = {
                callback:{
                    // 单击事件
                    onClick:zTreeOnClick,
                }
            };
            // zTree 的数据属性，深入使用请参考 API 文档（zTreeNode 节点数据详解）
            $(document).ready(function(){
               zTreeObj = $.fn.zTree.init($("#treeDemo"), setting, treeData);
            });
        });
        // 树结构单击事件
        function zTreeOnClick(event, treeId, treeNode) {
            //alert(treeNode.tId + ", " + treeNode.id);
            $("#departId").val(treeNode.id)
            $('#searchButton').click();
            //$('#searchForm').trigger("click");
        }
    });
</script>
</body>
</html>
